package com.oa.ylms.apps.dao.plan.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.plan.ICheckPlanDao;
import com.oa.ylms.apps.entity.plan.CheckPlanValue;

public class CheckPlanDaoImpl extends HibernateDaoSupport implements ICheckPlanDao {
	List<CheckPlanValue> list;
	int numbers;


	@Override
	public int addCheckPlan(CheckPlanValue checkPlan) throws Exception {
		if(checkPlan != null){
			this.getHibernateTemplate().save(checkPlan);
			return checkPlan.getPlanId();
		}
		return 0;
	}

	@Override
	public boolean editCheckPlan(CheckPlanValue checkPlan) throws Exception {
		if(checkPlan != null){
			try{
				this.getHibernateTemplate().update(checkPlan);
			}catch(Exception e){
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public CheckPlanValue queryByPlanId(CheckPlanValue checkPlan)
			throws Exception {
		if(checkPlan != null){
			return (CheckPlanValue) this.getHibernateTemplate().get(CheckPlanValue.class, checkPlan.getPlanId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CheckPlanValue> queryCheckPlanWithPage(final CheckPlanValue checkPlan ,final int rows ,final int pageNo)
			throws Exception {
		String hqlStr = "FROM CheckPlanValue cpv WHERE 1=1";
		if(checkPlan.getCheckPlanCategory() != null){
			hqlStr += " AND cpv.checkPlanCategory = ?";
		}
		if(checkPlan.getPostInfo() != null){
			hqlStr += " AND cpv.postInfo = ?";
		}
		hqlStr += " AND cpv.states <> ? ORDER BY cpv.checkPlanCategory";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(checkPlan.getCheckPlanCategory() != null){
					query.setEntity(++index, checkPlan.getCheckPlanCategory());
				}
				if(checkPlan.getPostInfo() != null){
					query.setEntity(++index, checkPlan.getPostInfo());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countTPage(final CheckPlanValue checkPlan, final int rows) throws Exception {
		int pageNumbers = 0;
		String hqlStr = "FROM CheckPlanValue cpv WHERE 1=1";
		if(checkPlan.getCheckPlanCategory() != null){
			hqlStr += " AND cpv.checkPlanCategory = ?";
		}
		if(checkPlan.getPostInfo() != null){
			hqlStr += " AND cpv.postInfo = ?";
		}
		hqlStr += " AND cpv.states <> ? ORDER BY cpv.checkPlanCategory";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(checkPlan.getCheckPlanCategory() != null){
					query.setEntity(++index, checkPlan.getCheckPlanCategory());
				}
				if(checkPlan.getPostInfo() != null){
					query.setEntity(++index, checkPlan.getPostInfo());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		 numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}
	
	public int getTotalNumber(){
		return numbers;
	}
	
	public List<CheckPlanValue> getList() {
		return list;
	}

	public void setList(List<CheckPlanValue> list) {
		this.list = list;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

}
